﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HRMSystemShi.DAL;
using HRMSystemShi.Model;

namespace HRMSystemShi._2025
{
    public partial class FormEmployeeEdit : Form
    {
        Employee employee = new Employee();
        bool isAdd = false;
        bool isUpdate = false;
        public FormEmployeeEdit()
        {
            isAdd = true;
            InitializeComponent();
            this.Text = "添加员工";
        }

        public FormEmployeeEdit(Guid id)
        {
            isUpdate = true;
            employee.Id = id;
            this.Text = "修改员工";
            InitializeComponent();
        }
        private void FormEmployeeAdd_Load(object sender, EventArgs e)
        {
            if (isUpdate)
            {
                textBoxName.Enabled = false;
                textBoxNumber.Enabled = false;
            }

            comboBoxEducation.DataSource = DictionaryService.GetDictionaryList(Category.学历);
            InitControl(comboBoxEducation);
            comboBoxGender.DataSource = DictionaryService.GetDictionaryList(Category.性别);
            InitControl(comboBoxGender);
            comboBoxMarriage.DataSource = DictionaryService.GetDictionaryList(Category.婚姻状况);
            InitControl(comboBoxMarriage);
            comboBoxParty.DataSource = DictionaryService.GetDictionaryList(Category.政治面貌);
            InitControl(comboBoxParty);
            comboBoxDepartment.DataSource = DepartmentService.GetDepartmentList();
            InitControl(comboBoxDepartment);

            if (employee.Id != Guid.Empty)
            {
                Employee employeeSelected = EmployeeService.GetEmployeeById(employee.Id);
                textBoxName.Text = employeeSelected.Name;
                textBoxNumber.Text = employeeSelected.Number;
                dateTimePickerBirthday.Value = employeeSelected.Birthday;
                dateTimePickerInDay.Value = employeeSelected.InDay;
                comboBoxMarriage.SelectedValue = employeeSelected.MarriageId;
                comboBoxParty.SelectedValue = employeeSelected.PartyId;
                comboBoxEducation.SelectedValue = employeeSelected.EducationId;
                comboBoxGender.SelectedValue = employeeSelected.GenderId;
                comboBoxDepartment.SelectedValue = employeeSelected.DepartmentId;
                textBoxTelephone.Text = employeeSelected.Telephone;
                textBoxAddress.Text = employeeSelected.Address;
                textBoxEmail.Text = employeeSelected.Email;
                textBoxRemarks.Text = employeeSelected.Remarks;
                textBoxResume.Text = employeeSelected.Resume;
                textBoxNation.Text = employeeSelected.Nation;
                textBoxNativePlace.Text = employeeSelected.NativePlace;
                if (employeeSelected.photo != null)
                {
                    pictureBoxPhoto.Image = new Bitmap(new MemoryStream(employeeSelected.photo));
                    employee.photo = employeeSelected.photo;
                }
            }
        }
        public void InitControl(ComboBox comboBox)
        {
            comboBox.DisplayMember = "Name";
            comboBox.ValueMember = "Id";
            comboBox.SelectedIndex = -1;
        }

        private void buttonSelectPhoto_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog openFileDialog = new OpenFileDialog())
            {
                openFileDialog.Filter = "所有文件|*.*";
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    string filePath = openFileDialog.FileName;
                    employee.photo = File.ReadAllBytes(filePath);
                    pictureBoxPhoto.Image = Image.FromFile(filePath);
                }
                else
                {
                    CommonHelper.MessageError("添加照片失败!!");
                }
            }
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            if (employee.Id == Guid.Empty)
            {
                employee.Id = Guid.NewGuid();
            }
            employee.Name = textBoxName.Text;
            employee.Number = textBoxNumber.Text;
            employee.Birthday = dateTimePickerBirthday.Value;
            employee.InDay = dateTimePickerInDay.Value;
            employee.MarriageId = (Guid)comboBoxMarriage.SelectedValue!;
            employee.PartyId = (Guid)comboBoxParty.SelectedValue!;
            employee.EducationId = (Guid)comboBoxEducation.SelectedValue!;
            employee.GenderId = (Guid)comboBoxGender.SelectedValue!;
            employee.DepartmentId = (Guid)comboBoxDepartment.SelectedValue!;
            employee.Telephone = textBoxTelephone.Text.Trim();
            employee.Address = textBoxAddress.Text.Trim();
            employee.Email = textBoxEmail.Text.Trim();
            employee.Remarks = textBoxRemarks.Text.Trim();
            employee.Resume = textBoxResume.Text.Trim();
            employee.Nation = textBoxNation.Text.Trim();
            employee.NativePlace = textBoxNativePlace.Text.Trim();
            if (isAdd)
            {
                if (EmployeeService.AddEmployee(employee))
                {
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
                else
                {
                    this.DialogResult = DialogResult.Cancel;
                }
            }
            else if (isUpdate)
            {
                if (EmployeeService.UpdateEmployee(employee))
                {
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
                else
                {
                    this.DialogResult = DialogResult.Cancel;
                }
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            if(CommonHelper.MessageYesOrNo("确定取消吗?") == DialogResult.Yes)
            {
                this.Close();
            }
            else
            {
                CommonHelper.MessageError("取消操作");
            }
        }
    }
}
